package com.beagle.njddapp.ui.activity.owner;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.ui.adapter.NjManagerListAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.DeviceManagerPresenter;
import com.beagle.njddapp.ui.view.DeviceManagerView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.beagle.njddapp.weight.dialog.ChePaiDIalog;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-02 22:31
 * 设备管理
 */
public class DeviceManagerActivity extends BaseMvpActivity<DeviceManagerView, DeviceManagerPresenter> implements DeviceManagerView, NjManagerListAdapter.NjListItemClickLisenter<NjBean>, ChePaiDIalog.onChePaiChooseCallback {
    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.device_reciclerview)
    RecyclerView recyclerView;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.prograssBar)
    ProgressBar progressBar;
    private List<NjBean> data;
    private NjManagerListAdapter adapter;
    private ChePaiDIalog chePaiDIalog;
    @Override
    public int getLayoutId() {
        return R.layout.activity_device_manager;
    }

    @Override
    public void init() {

        StatusBarUtil.immersive(this);
        StatusBarUtil.darkMode(this);
        StatusBarUtil.setPaddingSmart(this,container);
        tvTitle.setText("农机设备列表");
        container.setBackgroundColor(Color.WHITE);
        tvRight.setText("添加");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        data=new ArrayList<>();
        adapter=new NjManagerListAdapter(data,this);
        adapter.setOnItemClickLisenter(this);
        recyclerView.setAdapter(adapter);
        presenter.getDeviceListRun(1000,1);

    }

    @Override
    public DeviceManagerView getMvpView() {
        return this;
    }

    @Override
    public DeviceManagerPresenter createPresenter() {
        return new DeviceManagerPresenter();
    }

    @Override
    public void onError(String message) {
        progressBar.setVisibility(View.GONE);
        ToastUtils.showShort(message);
    }

    @OnClick({R.id.common_back_iv,R.id.common_tv_right})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.common_tv_right:
                if (chePaiDIalog==null){
                    chePaiDIalog=new ChePaiDIalog(this);
                    chePaiDIalog.setOnChePaiChooseCallback(this);
                }
                chePaiDIalog.show();
                break;
        }
    }

    @Override
    public void showRunNj(List<NjBean> results) {
        if (results==null){
            return;
        }
        data.clear();
        data.addAll(results);
        adapter.notifyDataSetChanged();
        tvCount.setText(results.size()+"");
    }

    @Override
    public void showdelete() {
        progressBar.setVisibility(View.GONE);
        ToastUtils.showShort("删除成功");
        presenter.getDeviceListRun(1000,1);

    }

    @Override
    public void showNumber(String result) {
        progressBar.setVisibility(View.GONE);
        if (TextUtils.isEmpty(result)){
            startActivityForResult(new Intent(this, DeviceAddActivity.class),334);
        }else {
            startActivityForResult(new Intent(this, DeviceAddActivity.class).putExtra("number",result),334);

        }
    }

    @Override
    public void onItemClickLisenter(Object njBean) {
        startActivity(new Intent(this,NjDetailActivity.class).putExtra("data",(NjBean)njBean));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==311){
            presenter.getDeviceListRun(1000,1);

        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        presenter.getDeviceListRun(1000,1);
    }

    @Override
    public void delete(NjBean njBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("是否删除该农机设备");
        //点击对话框以外的区域是否让对话框消失
        builder.setCancelable(true);
        //设置正面按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                progressBar.setVisibility(View.VISIBLE);
                presenter.deleteMeshBase(njBean.getMeshId()+"");

            }
        });
        //设置反面按钮
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

    }

    @Override
    public void noNj() {
        progressBar.setVisibility(View.VISIBLE);
        presenter.queryMeshNum();

    }

    @Override
    public void yesNj() {
        startActivityForResult(new Intent(this, DeviceAddActivity.class),334);

    }
}
